﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TiaoTiaoCode.AutoUpdate.Common.Dtos;
using TiaoTiaoCode.AutoUpdate.Common.Requests;
using TiaoTiaoCode.AutoUpdate.ServiceCore.IServices;
using TiaoTiaoCode.NLogger;

namespace TiaoTiaoCode.AutoUpdate.WebApi.Controllers
{
    /// <summary>
    /// Server端使用接口
    /// </summary>
    [AllowAnonymous]
    public class ServerUIController : ApiController
    {
        #region

        readonly IMainProgramVersionService _mainVersionSer;

        #endregion 

        /// <summary>
        /// 构造函数
        /// </summary>
        public ServerUIController(IMainProgramVersionService mainProgramVersionService)
        {
            _mainVersionSer = mainProgramVersionService;
        }

        /// <summary>
        /// 创建程序信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("createProgram")]
        public async Task<IActionResult> CreateProgram(MainProgramCreateDto dto)
        {
            return Success(await _mainVersionSer.CreateMainProgramInfo(dto));
        }

        /// <summary>
        /// 更新程序信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("updateProgram")]
        public async Task<IActionResult> UpdateProgram(MainProgramUpdateDto dto)
        {
            return Success(await _mainVersionSer.UpdateMainProgramInfo(dto));
        }

        /// <summary>
        /// 扫描程序文件
        /// </summary>
        /// <param name="programName"></param>
        /// <returns></returns>
        [HttpGet("scanFiles")]
        public async Task<IActionResult> ScanProgramFiles(string programName)
        {
            return Success(await _mainVersionSer.ScanMainProgramFiles(programName));
        }

        /// <summary>
        /// 获取需要更新的文件列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("programFiles")]
        public async Task<IActionResult> ProgramFiles(ProgramUpdateInfoRequest req)
        {
            return Success(await _mainVersionSer.ProgramFileDetails(req));
        }

        /// <summary>
        /// 获取应用程序维护列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("appList")]
        public async Task<IActionResult> MainProgramAppList()
        {
            return Success(await _mainVersionSer.MainProgramAppList());
        }

        /// <summary>
        /// 发布程序版本
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        [HttpPost("publish")]
        public async Task<IActionResult> PublishProgram(List<MainProgramView> items)
        {
            return Success(await _mainVersionSer.PublishProgram(items));
        }

        /// <summary>
        /// 查询版本信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("query")]
        public async Task<IActionResult> QueryVersion(string? programName)
        {
            return Success(await _mainVersionSer.QueryVersion(programName));
        }

        /// <summary>
        /// 上传更新包zip文件
        /// 文件上限1G
        /// </summary>
        /// <param name="programName"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("upload")]
        [RequestFormLimits(MultipartBodyLengthLimit = 1073741824)]
        [RequestSizeLimit(1073741824)]
        public async Task<IActionResult> UploadUpgradeZip([FromForm] string programName, IFormFile file)
        {
            #region 文件copy到临时路径

            var path = Path.GetTempPath();

            // 临时文件名
            var fullName = Path.Combine(path, $"{Guid.NewGuid():D}{Path.GetExtension(file.FileName)}");

            TiaoTiaoNLogger.LogInfo(fullName);

            using (var fs = new FileStream(fullName, FileMode.Create))
            {
                await file.CopyToAsync(fs);
                fs.Flush();
                fs.Close();
            }

            TiaoTiaoNLogger.LogInfo($"更新包文件:{fullName}");

            #endregion
            await _mainVersionSer.UploadZipFile(programName, fullName);
            return Success();
        }
    }
}
